from django.db import models
from django.utils import dateformat

from monitor_alert_center.models import MonitorTask, MonitorGroup


# Create your models here.

class MonitorTaskExecutionStatus(models.Model):
    """监控任务执行状态表"""
    name = models.ForeignKey(MonitorTask, on_delete=models.CASCADE, verbose_name="关联监控名称")
    type_choices = (
        (1, '站点检测'),
        (2, '端口检测'),
        (3, '进程检测'),
        (4, '自定义脚本'),
    )
    type = models.SmallIntegerField(null=True, choices=type_choices,
                                    verbose_name="监控类型(1：站点检测，2：端口检测，3：进程检测，4：自定义脚本)")
    group = models.ForeignKey(MonitorGroup, on_delete=models.CASCADE, verbose_name="关联监控分组名称")
    status = models.SmallIntegerField(verbose_name="监控任务执行状态(0:执行失败，1：执行成功)", db_index=True)
    create_time = models.DateTimeField(null=True, auto_now_add=True, verbose_name="创建时间", db_index=True)

    def formatted_create_time(self):
        return dateformat.format(self.create_time, 'Y/m/d H:i')  # 格式化为 "2025/04/08 18:06"

    class Meta:
        verbose_name = "监控任务执行状态表"
        db_table = "monitor_task_execution_status"
